package com.cimp.util.common;

import com.cimp.util.common.log.StreamGobbler;
import com.cimp.util.common.log.StreamType;

import java.io.File;
import java.io.IOException;

/**
 * [简要描述]:
 * [详细描述]:
 *
 * @author huojinhao
 * @date 2018/11/16
 */
public class ShellUtil {

    public static void exec(String cmd, String dir) throws Exception{
        String[] cmds = {"/bin/sh", "-c", cmd};
        Process process = null;

        if (dir == null) {
            process = Runtime.getRuntime().exec(cmds);
        }else{
            process = Runtime.getRuntime().exec(cmds, null, new File(dir));

        }

        StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), StreamType.ERROR);
        StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), StreamType.DEBUG);
        errorGobbler.start();
        outputGobbler.start();
        process.waitFor();
    }
}
